package com.manage.dao;

import com.manage.domain.Clazz;
import org.apache.ibatis.annotations.*;

import java.util.List;

public interface IClazzDao {

    @Select({
            "select c.*, e.name masterName,",
            "(case when begin_date > now() then '未开班' when now() > end_date then '已结课' else  '在读中' end ) status",
            "from clazz c left join emp e on c.master_id = e.id",
            "order by c.update_time desc"
    })
    List<Clazz> list();

    @Insert("insert into clazz(name, room, begin_date, end_date, master_id, subject) " +
            "values(#{name},#{room},#{beginDate},#{endDate},#{masterId},#{subject})")
    void insert(Clazz clazz);

    @Update({
            "<script>",
            "update clazz",
            "<set>",
            "<if test=\"name != null and name != ''\">name = #{name},</if>",
            "<if test=\"room != null and room != ''\">room = #{room},</if>",
            "<if test=\"beginDate != null\">begin_date = #{beginDate},</if>",
            "<if test=\"endDate != null\">end_date = #{endDate},</if>",
            "<if test=\"masterId != null\">master_id = #{masterId},</if>",
            "<if test=\"subject != null\">subject = #{subject},</if>",
            "<if test=\"updateTime != null\">update_time = #{updateTime}</if>",
            "</set>",
            "where id = #{id}",
            "</script>"
    })
    void update(Clazz clazz);

    @Delete("delete from clazz where id=#{id}")
    void deleteById(Integer id);

    @Select("select count(1) from student where clazz_id=#{clazzId}")
    int countStudents(Integer clazzId);
}


